/*
 * $Id: mxApplication.js,v 1.21 2010-11-08 20:09:40 gaudenz Exp $
 * Copyright (c) 2006-2010, JGraph Ltd
 *
 * Defines the startup sequence of the application.
 *
 */
{

	/**
	 * Constructs a new application (note that this returns an mxEditor
	 * instance).
	 */
	function mxApplication(config)
	{						
		try
		{
			if (!mxClient.isBrowserSupported())
			{
				mxUtils.error('Browser is not supported!', 200, false);
			}
			else
			{				
				mxConstraintHandler.prototype.pointImage = new mxImage('../images/dot.gif', 10, 10);
				//mxGraph.prototype.cellsEditable = false;				
	
				var vports = new Array();
				
				// NOTE: Constraint is used later for orthogonal edge routing (currently ignored)
				vports['w'] = {x: 0, y: 0.5, perimeter: true, constraint: 'west'};
				vports['e'] = {x: 1, y: 0.5, perimeter: true, constraint: 'east'};
				
				var hports = new Array();
				
				hports['n'] = {x: 0.5, y: 0, perimeter: true, constraint: 'north'};
				hports['s'] = {x: 0.5, y: 1, perimeter: true, constraint: 'south'};					
				
				// Returns all possible ports for a given terminal					
				
				//read the editor from config file
				var node = mxUtils.load(config).getDocumentElement();
				var editor = new mxEditor(node);

				editor.addAction("addRole", function(edt)
				{
					edt.showRoleWindow();
				});
																
				/*editor.graph.connectionHandler.isConnectableCell = function(cell)
				{
					if(cell.isPlace()) return true;
					else return false;
				};
				mxEdgeHandler.prototype.isConnectableCell = function(cell)
				{
					return graph.connectionHandler.isConnectableCell(cell);
				};*/
				
				// Disables existing port functionality
				editor.graph.view.getTerminalPort = function(state, terminal, source)
				{
					return terminal;
				};

				editor.graph.getAllConnectionConstraints = function(terminal, source)
				{
					if (terminal != null && terminal.shape != null &&
						terminal.shape instanceof mxStencilShape)
					{
						// for stencils with existing constraints...
						if (terminal.shape.stencil != null)
						{
							return terminal.shape.stencil.constraints;
						}
					}
					else if (terminal != null && terminal.cell.getAttribute("elementType") == "Transition")
					{
						var ports;
						if(terminal.cell.getAttribute("shapeLabel") == "HTransition") ports = hports;
						else if(terminal.cell.getAttribute("shapeLabel") == "VTransition") ports = vports;
						var cstrs = new Array();
						
						for (var id in ports)
						{
							var port = ports[id];
							
							var cstr = new mxConnectionConstraint(new mxPoint(port.x, port.y), port.perimeter);
							cstr.id = id;
							cstrs.push(cstr);
						}
						
						return cstrs;
					}					
					return null;
				};			
				// Updates the window title after opening new files
				var title = document.title;
				var funct = function(sender)
				{
					document.title = title + ' - ' + sender.getTitle();
				};
				
				editor.addListener(mxEvent.OPEN, funct);
				
				// Prints the current root in the window title if the
				// current root of the graph changes (drilling).
				editor.addListener(mxEvent.ROOT, funct);
				funct(editor);
				
				// Displays version in statusbar
				editor.setStatus('mxGraph '+mxClient.VERSION);						
				
				//add user-defined actions
				editor.addAction("addToken", function (edt){
					var graph = edt.graph;
					var cnt = graph.getSelectionCount();
					var cell = graph.getSelectionCell();				
					if(cnt == 1)
					{
						/*if(cell.getAttribute("elementType") == "AEPlace" 
							|| cell.getAttribute("elementType") == "TPlace")
						{
							cell.setAttribute("tokenNum",parseInt(cell.getAttribute("tokenNum"))+1);							
						}*/
						mxLog.show();
						mxLog.debug(edt.encode(cell));						
					}
				});				
				
				var currentView = "graph";
				var textNode = document.getElementById('xml');
				var graphNode = editor.graph.container;
				editor.addAction("switchView", function(edt)
				{					
					mxLog.show();
					//mxLog.debug(mxUtils.getPrettyXml(edt.getXmlRoot()));
					var enc = new mxCodec();
					var node = enc.encode(editor.graph.getModel());
					mxLog.debug(mxUtils.getPrettyXml(node));
					
					/*if(currentView == "graph")
					{
						currentView = "source";
						graphNode.style.display = 'none';
						textNode.style.display = 'inline';
						
						var enc = new mxCodec();
						var node = enc.encode(editor.graph.getModel());
						
						textNode.value = mxUtils.getPrettyXml(node);
						textNode.originalValue = textNode.value;
						textNode.focus();
					}
					else if(currentView == "source")
					{
						currentView = "graph";
						graphNode.style.display = '';
					
						if (textNode.value != textNode.originalValue)
						{
							var doc = mxUtils.parseXml(textNode.value);
							var dec = new mxCodec(doc);
							dec.decode(doc.documentElement, editor.graph.getModel());
						}

						textNode.originalValue = null;
						
						// Makes sure nothing is selected in IE
						if (mxClient.IS_IE)
						{
							mxUtils.clearSelection();
						}

						textNode.style.display = 'none';

						// Moves the focus back to the graph
						textNode.blur();
						editor.graph.container.focus();
						
					}*/
				});

				// Shows the application
				hideSplash();
			}
		}
		catch (e)
		{
			hideSplash();
			// Shows an error message if the editor cannot start
			mxUtils.alert('Cannot start application: '+e.message);
			throw e; // for debugging
		}
								
		return editor;
	}
}
